package com.polaris.live.adapter.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.adapter.live.BannerViewHolder
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.ext.convertArabicDigitsToNormal
import com.polaris.live.databinding.AdapterDynamicBinding
import com.polaris.live.databinding.AdapterListBannerBinding
import com.polaris.live.resp.back_resp.ListResultCommunityListResp
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.avatar.FrameImageUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * DynamicAdapter
 *
 * @author Created by 半仙 on 2024/9/4/004 10:16
 */
class DynamicAdapter : BaseAdapter<Any, BaseViewHolder>(0) {

    init {
        addChildClickViewIds(R.id.ll_head, R.id.iv_say_hi, R.id.iv_user_img, R.id.iv_nice, R.id.tv_content)
    }

    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is ListResultCommunityListResp -> CommonConst.HomeListType.TYPE_NORMAL
            is ListWithBannerBean -> CommonConst.HomeListType.TYPE_BANNER
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            CommonConst.HomeListType.TYPE_BANNER -> BannerViewHolder(
                AdapterListBannerBinding.inflate(layoutInflater, parent, false)
            )

            CommonConst.HomeListType.TYPE_NORMAL -> DynamicViewHolder(
                AdapterDynamicBinding.inflate(layoutInflater, parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is DynamicViewHolder -> if (item is ListResultCommunityListResp) holder.bind(item)
            is BannerViewHolder -> if (item is ListWithBannerBean) holder.bind(item)
        }
    }

    class DynamicViewHolder(binding: AdapterDynamicBinding) :
        BaseBindingViewHolder<AdapterDynamicBinding, ListResultCommunityListResp>(binding) {

        override fun bind(item: ListResultCommunityListResp) {
            mBinding.apply {
                avHead.setImageUrl(item.avatar, item.userId)
                avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(item.avatarFrame))
                tvNickName.text = item.nickName
                tvNickName.setNicknameColor(
                    null,
                    R.color.color_2e2e2e
                )
                if (CommonUtils.isMan(item.sex)) {
                    widgetLevel.setRichsLevel(item.richs)
                } else {
                    widgetLevel.setCharmsLevel(item.charms)
                }
                widgetAge.setSexAndAge(item.sex, item.birthday)

                val (showContent, showImage) = if (item.translate.isNullOrEmpty()) {
                    item.content to R.drawable.dynamic_not_translate
                } else {
                    item.translate to R.drawable.dynamic_translate
                }
                vipView.setVipLevel(item.vipLevel)
                val span = SpanUtils.with(tvContent)
                    .append(showContent.toString())
                    .appendImage(showImage)
                    .create()
                tvContent.text = span
                if (item.picUrl?.isNotEmpty() == true) {
                    Glide.with(ivUserImg).load(ImageUtils.imgPath(item.picUrl))
                        .placeholder(R.drawable.dynamic_loading)
                        .error(R.drawable.dynamic_loading)
                        .transform(CenterCrop(), RoundedCorners(8.autoDp))
                        .into(ivUserImg)
                    ivUserImg.visibility = View.VISIBLE
                } else {
                    ivUserImg.visibility = View.GONE
                }


                tvNiceNum.text = "${item.likes}"

                tvTime.text = TimeUtils.millis2String(item.createTime, "yyyy/MM/dd")
                    .convertArabicDigitsToNormal()

                if (item.like.toBoolean()) {
                    tvNiceNum.setTextColor(ColorUtils.getColor(R.color.dynamicLikeTextColor))
                    ivNice.setImageResource(R.drawable.dynamic_nick)
                } else {
                    tvNiceNum.setTextColor(ColorUtils.getColor(R.color.dynamicNoLikeTextColor))
                    ivNice.setImageResource(R.drawable.dynamic_not_nick)
                }
            }
        }

    }

}